﻿#include "NCTDialogUserDefined.h"
#include "ui_NCTDialogUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include <QMessageBox>

namespace GUI
{
    NCTDialogUserDefined::NCTDialogUserDefined(PhysicsDlgExecType type, int nctid/*=-1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type, parent)
    {
        _ui = new Ui::NCTDialogUserDefined;
        _ui->setupUi(this);

        initComboBox(_ui->comboBox_absorptionCoelfficient, 1);
        initComboBox(_ui->comboBox_ILAR, 2);
        initComboBox(_ui->comboBox_ILANR, 2);
        initComboBox(_ui->comboBox_FWD, 3);
        initComboBox(_ui->comboBox_EWD, 3);
        initComboBox(_ui->comboBox_SWD, 3);
        //获取声学包对象
        if (_nctMgr && nctid > 0)
            _nct = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(_nctMgr->getDataByID(nctid));
        this->init();
    }

    NCTDialogUserDefined::~NCTDialogUserDefined()
    {
        if (_ui) delete _ui;
    }

    void NCTDialogUserDefined::on_pushButton_OK_clicked()
    {
        if (!this->checkInput()) return;
        Acoustics::FITKAcousticsNCTUserDefined* ud = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(_nct);
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            ud = new Acoustics::FITKAcousticsNCTUserDefined;
            _nctMgr->appendDataObj(ud);
        }
        if (!ud) return;
        ud->setDataObjectName(_ui->lineEdit_name->text());
        ud->setAbsorptionCoeffID(_ui->comboBox_absorptionCoelfficient->currentData().toInt());
        ud->setInsertionLossResonantID(_ui->comboBox_ILAR->currentData().toInt());
        ud->setInsertionLossNonResonantID(_ui->comboBox_ILANR->currentData().toInt());
        ud->setDampingFlexureID(_ui->comboBox_FWD->currentData().toInt());
        ud->setDampingExtensionID(_ui->comboBox_EWD->currentData().toInt());
        ud->setDampingShearID(_ui->comboBox_SWD->currentData().toInt());
        ud->setMassPerUnitArea(_ui->lineEdit_massPerUnitArea->text().toDouble());
        this->accept();
    }

    void NCTDialogUserDefined::on_pushButton_cancel_clicked()
    {
        this->reject();
    }

    void NCTDialogUserDefined::init()
    {
        if (_nctMgr == nullptr) return;
        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _nctMgr->checkName("New User-defined Treatment");
            _ui->lineEdit_name->setText(name);
        }
        Acoustics::FITKAcousticsNCTUserDefined* ud = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(_nct);
        if (!ud) return;

        _ui->lineEdit_name->setText(ud->getDataObjectName());

        int id1 = ud->getAbsorptionCoeffID();
        int index1 = _ui->comboBox_absorptionCoelfficient->findData(id1);
        _ui->comboBox_absorptionCoelfficient->setCurrentIndex(index1);

        int id2 = ud->getInsertionLossResonantID();
        int index2 = _ui->comboBox_ILAR->findData(id2);
        _ui->comboBox_ILAR->setCurrentIndex(index2 );

        int id3 = ud->getInsertionLossNonResonantID();
        int index3 = _ui->comboBox_ILANR->findData(id3);
        _ui->comboBox_ILANR->setCurrentIndex(index3 );

        int id4 = ud->getDampingFlexureID();
        int index4 = _ui->comboBox_FWD->findData(id4);
        _ui->comboBox_FWD->setCurrentIndex(index4);

        int id5 = ud->getDampingExtensionID();
        int index5 = _ui->comboBox_EWD->findData(id5);
        _ui->comboBox_EWD->setCurrentIndex(index5);

        int id6 = ud->getDampingShearID();
        int index6 = _ui->comboBox_SWD->findData(id6);
        _ui->comboBox_SWD->setCurrentIndex(index6);

        _ui->lineEdit_massPerUnitArea->setText(QString::number(ud->getMassPerUnitArea()));

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(ud->getDataObjectName());
            _ui->lineEdit_name->setText(text);
        }
    }

    bool NCTDialogUserDefined::checkInput()
    {
        QString text1 = _ui->lineEdit_massPerUnitArea->text().trimmed();
        bool ok = false;
        text1.toDouble(&ok);
        if (!ok)
        {
            QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
            return false;
        }
        if (_nctMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_name->text().trimmed();
        if (text != "") {
            QString newText = _nctMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_nctMgr->getDataByName(text) == _nct) return true;
        }
        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }

    void NCTDialogUserDefined::initComboBox(QComboBox *box, int type)
    {
        box->clear();
        Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phy == nullptr) return;
        Acoustics::FITKAcousticsSpectraManager *specmgr=phy->getFITKAcousticsSpectraManager();
        if (!specmgr)return;
        QList<Acoustics::FITKAcousticsAbstractSpectra*> list{};
        if (type == 1)
            list = specmgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Absorption);
        else if (type == 2)
            list = specmgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::InsertionLoss);
        else if (type == 3)
            list = specmgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Damping);
        if (list.isEmpty()) return;
        if (type == 3)box->addItem("None", -1);
        for (auto mate : list) {
            box->addItem(mate->getDataObjectName(), mate->getDataObjectID());
        }
    }
}